import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;


class P10959_39643 {

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString());
	}
	
	public static void main(String[] args) {
		int cases = Integer.parseInt(readLn());
		
		for (int kase = 0; kase < cases; kase++) {
			readLn();
			StringTokenizer st = new StringTokenizer(readLn());
			int p = Integer.parseInt(st.nextToken());
			int d = Integer.parseInt(st.nextToken());
			
			HashMap<Integer, Node10959> graph = new HashMap<Integer, Node10959>();
			
			for (int k = 0; k < p; k++) {
				Node10959 node = new Node10959();
				node.id = k;
				graph.put(k, node);
			}
			
			for (int i = 0; i < d; i++) {
				st = new StringTokenizer(readLn());
				Node10959 x = graph.get(Integer.parseInt(st.nextToken()));
				Node10959 y = graph.get(Integer.parseInt(st.nextToken()));
				x.nexts.add(y);
				y.nexts.add(x);
			}
			
			
			List<Node10959> queue = new LinkedList<Node10959>();
			graph.get(0).marked = true;
			queue.addAll(graph.get(0).nexts);
			
			for (Node10959 n : queue) { 
				n.number = 1;
				n.marked = true;
			}
			
			while (!queue.isEmpty()) {
				
				Node10959 n = queue.remove(0);
				
				for (Node10959 no : n.nexts) {
					if (!no.marked) {
						no.marked = true;
						no.number = n.number + 1;
						queue.add(no);
					}
				}
				
			}
			
			StringBuilder sb = new StringBuilder();
			for (int i = 1; i < p; i++) {
				sb.append(graph.get(i).number + "\n");
			}
			
			System.out.print(sb.toString());
			
			if (kase + 1 != cases) System.out.println();
			
		}
		
	}



}

class Node10959 {
	boolean marked = false;
	int id;
	HashSet<Node10959> nexts = new HashSet<Node10959>();
	int number;
}
